(function() {
    'use strict';
    angular.module('weApp.controllers')
            //换乘
            .controller('transCtrl', ['$scope', '$rootScope', '$state', '$stateParams', '$timeout', '$http', 'weAppDataBase', 'showLinePageService', 'localStorageService', 'myFavorsService', '$ionicPopup', '$ionicLoading',
                function($scope, $rootScope, $state, $stateParams, $timeout, $http, weAppDataBase, showLinePageService, localStorageService, myFavorsService, $ionicPopup, $ionicLoading) {
                    $rootScope.lineMoney = JSON.parse(localStorageService.get("lineMoney"));
                    if (!$rootScope.lineMoney) {
                        var json = './data/money.json';
                        $http.get(json).success(function(data) {
                            $rootScope.lineMoney = data;
                            localStorageService.set("lineMoney", JSON.stringify(data));
                        });
                    }


                    if (!$rootScope.myAddress) {
                        if (localStorageService.get("myAddress") !== null) {
                            $rootScope.myAddress = JSON.parse(localStorageService.get("myAddress"));
                        }
                    }
                    if ($rootScope.myAddress && !$rootScope.sAddr) {
                        // $rootScope.sAddr = "我的位置";
                        $rootScope.sAddr = {
                            sta: "我的位置",
                            addr: $rootScope.myAddress,
                            end: ""
                        }
                    }
                    if (!$rootScope.sAddr) {
                        $rootScope.sAddr = {};
                    }
                    localStorageService.set("sAddr", JSON.stringify($rootScope.sAddr));
                    if ($stateParams.data || $rootScope.serLoc) {
                        var data = $stateParams.data || $rootScope.serLoc;
                        if (data.sName)
                            // $('#txtBusSta').val(data.sName);
                            $rootScope.sAddr.sta = data.sName;
                        if (data.eName)
                            // $('#txtBusEnd').val(data.eName);
                            $rootScope.sAddr.end = data.eName;
                    }
                    if (!$rootScope.serLoc) {
                        $rootScope.serLoc = {};
                    }
                    $scope.clickSta = function() {
                        console.log('clickSta');
                        $state.go('location', {tit: '请输入起点', tag: 'sta'});
                    }
                    $scope.clickEnd = function() {
                        console.log('clickEnd');
                        $state.go('location', {tit: '请输入终点', tag: 'end'});
                    }
                    /*$scope.clickEnd = function () {
                     console.log('clickEnd');
                     $state.go('transfer', {sta: '宝塔区第一中学', end: '延安第四中学'});
                     }*/
                    $scope.sTransit = function(localTran) {
                        var arr = [];
                        var temp = null;
                        for (var m in localTran) {
                            if (m >= 0) {
                                arr.push(JSON.parse(localTran[m]));
                            }
                        }

                        if (arr.length) {
                            $scope.transit = arr;
                        } else {
                            $scope.transit = [];
                        }
                    }

                    if (localStorageService.get("transitData") !== null) {
                        var localTran = localStorageService.get("transitData") == null ? [] : JSON.parse(localStorageService.get("transitData")).reverse();
                        $scope.sTransit(localTran);
                    } else {
                        localStorageService.set("transit", null);
                        localStorageService.set("transitData", null);
                    }

                    //清空查询历史事件
                    $scope.delSearchConfirm = function() {
                        $ionicPopup.confirm({
                            title: "清空搜索记录",
                            template: "确认清空全部搜索记录吗？",
                            cancelText: "取消",
                            okText: "确定",
                            okType: "button-balanced"
                        }).then(function(res) {
                            if (res) {
                                localStorageService.set("transit", null);
                                localStorageService.set("transitData", null);
                                $scope.transit = [];
                                console.log("clear all histroy!");
                            } else {
                                console.log("clear all histroy has been canceled!");
                            }
                        })
                    }

                    $scope.clickList = function(data) {
                        $rootScope.sAddr.sta = data.sName;
                        $rootScope.sAddr.end = data.eName;
                        $rootScope.serLoc = data;
                        $state.go('transfer', {data: data});
                    }
                    $scope.serChange = function() {
                        var serLoc = null;
                        if ($rootScope.sAddr) {
                            if ($rootScope.sAddr.sta == '我的位置') {
                                if ($rootScope.serLoc.eName) {
                                    $rootScope.sAddr.sta = $rootScope.serLoc.eName;
                                    $rootScope.sAddr.end = '我的位置';
                                    serLoc = {
                                        sta: $rootScope.serLoc.end,
                                        sName: $rootScope.serLoc.eName,
                                        end: $rootScope.sAddr,
                                        eName: $rootScope.sAddr.name
                                    }
                                    $rootScope.serLoc = serLoc;
                                    $state.go('transfer', {data: $rootScope.serLoc});
                                    return false;
                                } else {
                                    $rootScope.sAddr.sta = '';
                                    $rootScope.sAddr.end = '我的位置';
                                }
                            } else if ($rootScope.sAddr.end == '我的位置') {
                                if ($rootScope.serLoc.sName) {
                                    $rootScope.sAddr.end = $rootScope.serLoc.sName;
                                    $rootScope.sAddr.sta = '我的位置';
                                    serLoc = {
                                        end: $rootScope.serLoc.end,
                                        eName: $rootScope.serLoc.eName,
                                        sta: $rootScope.sAddr,
                                        sName: $rootScope.sAddr.name
                                    }
                                    $rootScope.serLoc = serLoc;
                                    $state.go('transfer', {data: $rootScope.serLoc});
                                    return false;
                                } else {
                                    $rootScope.sAddr.sta = '我的位置';
                                    $rootScope.sAddr.end = '';
                                }
                            } else {
                                if ($rootScope.serLoc) {
                                    if ($rootScope.serLoc.sName && $rootScope.serLoc.eName) {
                                        $rootScope.sAddr.sta = $rootScope.serLoc.eName;
                                        $rootScope.sAddr.end = $rootScope.serLoc.sName;
                                        serLoc = {
                                            sta: $rootScope.serLoc.end,
                                            sName: $rootScope.serLoc.eName,
                                            end: $rootScope.serLoc.sta,
                                            eName: $rootScope.serLoc.sName
                                        }
                                        $rootScope.serLoc = serLoc;
                                        $state.go('transfer', {data: $rootScope.serLoc});
                                        return false;
                                    } else if ($rootScope.serLoc.sName) {
                                        $rootScope.sAddr.end = $rootScope.serLoc.sName;
                                        $rootScope.sAddr.sta = '';
                                        serLoc = {
                                            end: $rootScope.serLoc.sta,
                                            eName: $rootScope.serLoc.sName
                                        }
                                        $rootScope.serLoc = serLoc;
                                        return false;
                                    } else if ($rootScope.serLoc.eName) {
                                        $rootScope.sAddr.sta = $rootScope.serLoc.eName;
                                        $rootScope.sAddr.end = '';
                                        serLoc = {
                                            sta: $rootScope.serLoc.end,
                                            sName: $rootScope.serLoc.eName
                                        }
                                        $rootScope.serLoc = serLoc;
                                        return false;
                                    }
                                }
                            }
                        } else {
                            // if ($rootScope.serLoc.sName && $rootScope.serLoc.eName)
                            $rootScope.sAddr = {};
                            if ($rootScope.serLoc) {
                                if ($rootScope.serLoc.sName && $rootScope.serLoc.eName) {
                                    $rootScope.sAddr.sta = $rootScope.serLoc.eName;
                                    $rootScope.sAddr.end = $rootScope.serLoc.sName;
                                    serLoc = {
                                        sta: $rootScope.serLoc.end,
                                        sName: $rootScope.serLoc.eName,
                                        end: $rootScope.serLoc.sta,
                                        eName: $rootScope.serLoc.sName
                                    }
                                    $rootScope.serLoc = serLoc;
                                    $state.go('transfer', {data: $rootScope.serLoc});
                                    return false;
                                } else if ($rootScope.serLoc.sName) {
                                    $rootScope.sAddr.end = $rootScope.serLoc.sName;
                                    $rootScope.sAddr.sta = '';
                                    serLoc = {
                                        end: $rootScope.serLoc.sta,
                                        eName: $rootScope.serLoc.sName
                                    }
                                    $rootScope.serLoc = serLoc;
                                    return false;
                                } else if ($rootScope.serLoc.eName) {
                                    $rootScope.sAddr.sta = $rootScope.serLoc.eName;
                                    $rootScope.sAddr.end = '';
                                    serLoc = {
                                        sta: $rootScope.serLoc.end,
                                        sName: $rootScope.serLoc.eName
                                    }
                                    $rootScope.serLoc = serLoc;
                                    return false;
                                }
                            }
                        }
                    }
                    $scope.serClick = function() {
                        console.log('serClick');
                        if ($rootScope.serLoc.sName && $rootScope.serLoc.eName) {
                            $state.go('transfer', {data: $rootScope.serLoc});
                            return false;
                        } else {
                            $ionicLoading.show({
                                template: '请选择好起止站点后再试'
                            })

                            $timeout(function() {
                                $ionicLoading.hide();
                            }, 2000);
                            return false;
                        }

                    }
                }]);
})();